package com.mapfinal.server.auth;

import com.lambkit.Lambkit;
import com.mapfinal.server.auth.model.User;

public class AuthLoginCache {
	
	public static final String AUTH_USER_CACHE_NAME = "auth-user-cahce";
	
	public static void login(User user) {
		if(user==null) return;
		user.setLogin(true);
		Lambkit.getCache().put(AUTH_USER_CACHE_NAME, "login-" + user.getUserLogin(), user, 60*60*1000);//1小时
	}
	
	public static User getLoginUser(String name) {
		User user = Lambkit.getCache().get(AUTH_USER_CACHE_NAME, "login-" + name);
		if(user==null) {
			user = User.service().findByUserName(name);
			if(user==null) return user;
			user.setLogin(true);
			Lambkit.getCache().put(AUTH_USER_CACHE_NAME, "login-" + name, user, 24*60*60*1000);//1小时
		}
		return user;
	}
	
	public static void logout(User user) {
		Lambkit.getCache().remove(AUTH_USER_CACHE_NAME, "login-" + user.getUserLogin());
	}

	public static void logout(String userName) {
		Lambkit.getCache().remove(AUTH_USER_CACHE_NAME, "login-" + userName);
	}
}
